/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.service.clusterhandler;

import com.asiainfo.redis.entity.ClusterNode;
import com.asiainfo.redis.entity.ClusterNodeConfig;
import com.asiainfo.redis.service.HostService;
import com.asiainfo.redis.service.RedisService;
import org.springframework.data.redis.connection.RedisClusterConnection;
import java.util.List;

/**
 * @version v 1.0 on 2016/5/19 16:21
 * @auther william.xu
 */
public class AddNodes {

    private RedisClusterHandler redisClusterHandler;
    private HostService hostService;

    private ClusterNode existNode;
    private List<ClusterNode> masterNodes;
    private ClusterNodeConfig redisClusterNodeConfig;

    public AddNodes(ClusterNode existNode, List<ClusterNode> masterNodes, ClusterNodeConfig redisClusterNodeConfig,
                    RedisClusterHandler redisClusterHandler, HostService hostService){
        this.existNode = existNode;
        this.masterNodes = masterNodes;
        this.redisClusterNodeConfig = redisClusterNodeConfig;
        this.redisClusterHandler = redisClusterHandler;
        this.hostService = hostService;
    }

    public Object doInRedisCluster(RedisClusterConnection connection) throws Exception {
        redisClusterHandler.checkClusterNodes(connection);
        //2、根据redisClusterNodeConfig，ssh到目标host（遍历所有的masternode和slavenode）上执行生成集群节点的初始化脚本，
        // 并启动对应的node
        for (ClusterNode masterNode : masterNodes) {
            hostService.initRedisNode(redisClusterNodeConfig, masterNode);
            hostService.startRedisNode(masterNode);
            List<ClusterNode> slaveNodes = masterNode.getSlaveNodes();
            for (ClusterNode slaveNode : slaveNodes) {
                hostService.initRedisNode(redisClusterNodeConfig, slaveNode);
                hostService.startRedisNode(slaveNode);
            }
        }
        Thread.sleep(RedisService.SLEEP_TIME);
        //3、redis-cli到集群中一个存在的节点，对新添加的节点进行meet操作
        //Set<RedisNode> redisNodes = redisClusterConfiguration.getClusterNodes();
        //RedisNode redisNode = redisNodes.iterator().next();
        redisClusterHandler.meet(existNode, masterNodes);
        //meet操作后，需要等待集群join完成
        redisClusterHandler.checkAllMeeting(connection);
        //4、分配slave节点
        redisClusterHandler.replicate(masterNodes, connection);
        //5、把原来的slot平均迁移到新的master node 数据迁移独立一个动作
        //Iterator<RedisClusterNode> iterator = redisClusterConnection.clusterGetNodes().iterator();
        //Set<RedisClusterNode> masterClusterNode = redisClusterConnection.clusterGetMasterSlaveMap().keySet();
        //
        return null;

    }
}
